package gui;

import net.BackClient;
import util.Parser;
import util.Protocol;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class LogInUI extends JFrame {
    BackClient backClient = new BackClient();
    Protocol protocol = new Protocol();
    Parser parser = new Parser();
    private JPanel contentPane;
    private JTextField userField;
    private JPasswordField passwordField;

    public LogInUI() {
        setTitle("登录");
        getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 304, 162);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("账号：");
        label.setFont(new Font("宋体", Font.PLAIN, 17));
        label.setBounds(20, 29, 51, 24);
        contentPane.add(label);

        JLabel label_1 = new JLabel("密码：");
        label_1.setFont(new Font("宋体", Font.PLAIN, 17));
        label_1.setBounds(20, 63, 56, 25);
        contentPane.add(label_1);

        userField = new JTextField();
        userField.setBounds(81, 32, 78, 21);
        contentPane.add(userField);
        userField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(81, 67, 78, 21);
        contentPane.add(passwordField);

        JButton loginBtn = new JButton("登录");
        loginBtn.addActionListener(arg0 -> {
            String nickname = userField.getText();
            String password = String.valueOf(passwordField.getPassword());
            String information = nickname + "," + password;

            String massage = protocol.getLogInInformation(information);
            backClient.SendMassage(massage);
            String receive = backClient.receiveMassage();

            System.out.println(receive);
            String[] temp = receive.split(";");
            String head = temp[0];
            boolean isSuccess = parser.isLogin(head);
            if (isSuccess) {
                String friendID = temp[1];
                LogInUI.this.setVisible(false);
                // 显示朋友列表
                FriendListUi friendListUi = new FriendListUi();
                friendListUi.setVisible(true);
                friendListUi.setFriendID(friendID);
                friendListUi.setNickname(nickname);
            } else {
                JOptionPane.showMessageDialog(null, "账号密码错误");
            }
        });
        loginBtn.setBounds(187, 65, 95, 25);
        contentPane.add(loginBtn);

        JLabel lblNewLabel = new JLabel("登录");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel.setBounds(122, 7, 62, 15);
        contentPane.add(lblNewLabel);

        JButton registerBtn = new JButton("注册");
        registerBtn.addActionListener(arg0 -> {
            RegisterUI registerUI = new RegisterUI();
            registerUI.setVisible(true);
            LogInUI.this.setVisible(false);
        });
        registerBtn.setBounds(187, 30, 95, 25);
        contentPane.add(registerBtn);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                LogInUI frame = new LogInUI();
                frame.setVisible(true);
                frame.setResizable(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
